package com.gwtsz.chart.output.customer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.gwtsz.chart.R;
import com.gwtsz.chart.charts.LineChart;
import com.gwtsz.chart.components.YAxis;
import com.gwtsz.chart.data.CombinedData;
import com.gwtsz.chart.data.Entry;
import com.gwtsz.chart.data.LineData;
import com.gwtsz.chart.highlight.Highlight;
import com.gwtsz.chart.output.bean.DataParse;
import com.gwtsz.chart.output.utils.ChartThemeUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：spence
 * 邮箱：1025065158
 * 博客：http://blog.csdn.net/qqyanjiang
 */
public class MyLineChart extends LineChart {
    private MyLeftMarkerView myMarkerViewLeft;
    private MyRightMarkerView myMarkerViewRight;
    private MyBottomMarkerView mMyBottomMarkerView;
    private DataParse minuteHelper;
    private Context mContext;

    public MyLineChart(Context context) {
        super(context);
        mContext=context;
    }

    public MyLineChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
    }

    public MyLineChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
    }

    @Override
    protected void init() {
        super.init();
        /*此两处不能重新示例*/
        mXAxis = new MyXAxis();
        mAxisLeft = new MyYAxis(YAxis.AxisDependency.LEFT);
        mXAxisRenderer = new MyXAxisRenderer(mViewPortHandler, (MyXAxis) mXAxis, mLeftAxisTransformer, this);
        mAxisRendererLeft = new MyYAxisRenderer(mViewPortHandler, (MyYAxis) mAxisLeft, mLeftAxisTransformer);

        mAxisRight = new MyYAxis(YAxis.AxisDependency.RIGHT);
        mAxisRendererRight = new MyYAxisRenderer(mViewPortHandler, (MyYAxis) mAxisRight, mRightAxisTransformer);

    }

    /*返回转型后的左右轴*/
    @Override
    public MyYAxis getAxisLeft() {
        return (MyYAxis) super.getAxisLeft();
    }

    @Override
    public MyXAxis getXAxis() {
        return (MyXAxis) super.getXAxis();
    }


    @Override
    public MyYAxis getAxisRight() {
        return (MyYAxis) super.getAxisRight();
    }

    public void setMarker(MyLeftMarkerView markerLeft, MyRightMarkerView markerRight,MyBottomMarkerView markerBottom) {
        this.myMarkerViewLeft = markerLeft;
        this.myMarkerViewRight = markerRight;
        this.mMyBottomMarkerView=markerBottom;

        myMarkerViewRight.setTextColor(0, ChartThemeUtil.instance().color_chart_cursor);
        mMyBottomMarkerView.setTextColor(0, ChartThemeUtil.instance().color_chart_cursor);
        myMarkerViewRight.setTextColor(0, ChartThemeUtil.instance().color_chart_cursor);
    }

    public void setMarker(MyLeftMarkerView markerLeft, MyRightMarkerView markerRight,MyBottomMarkerView markerBottom, DataParse minuteHelper) {
        this.myMarkerViewLeft = markerLeft;
        this.myMarkerViewRight = markerRight;
        this.mMyBottomMarkerView=markerBottom;
        this.minuteHelper = minuteHelper;

        myMarkerViewRight.setTextColor(0, ChartThemeUtil.instance().color_chart_cursor);
        mMyBottomMarkerView.setTextColor(0, ChartThemeUtil.instance().color_chart_cursor);
        myMarkerViewRight.setTextColor(0, ChartThemeUtil.instance().color_chart_cursor);
    }

    public void setHighlightValue(Highlight h) {
        if (mData == null)
            mIndicesToHighlight = null;
        else {
            mIndicesToHighlight = new Highlight[]{
                    h};
        }
        invalidate();
    }

    @Override
    protected void drawMarkers(Canvas canvas) {
        drawPushLine(canvas);
        if (!mDrawMarkerViews || !valuesToHighlight())
            return;
        for (int i = 0; i < mIndicesToHighlight.length; i++) {
            Highlight highlight = mIndicesToHighlight[i];
            int xIndex = mIndicesToHighlight[i].getXIndex();
            int dataSetIndex = mIndicesToHighlight[i].getDataSetIndex();
            float deltaX = mXAxis != null
                    ? mXAxis.mAxisRange
                    : ((mData == null ? 0.f : mData.getXValCount()) - 1.f);
            if (xIndex <= deltaX && xIndex <= deltaX * mAnimator.getPhaseX()) {
                //Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]);
                Entry e=new Entry(mIndicesToHighlight[i].getValue(),mIndicesToHighlight[i].getXIndex());
                // make sure entry not null
                if (e == null || e.getXIndex() != mIndicesToHighlight[i].getXIndex())
                    continue;

                float[] pos = getMarkerPosition(e, highlight);
                // check bounds
                if (!mViewPortHandler.isInBounds(pos[0], pos[1]))
                    continue;

                //float yValForXIndex1 = mIndicesToHighlight[i].getValue();
                float yValForXIndex2 = mIndicesToHighlight[i].getValue();
                if(minuteHelper.getProductCode()==1){

                }
                String time;
                /**
                 * 切换模式高亮取消
                 */
                try {
                    time = minuteHelper.getDatas().get(mIndicesToHighlight[i].getXIndex()).time;
                }catch (Exception exception){
                    exception.printStackTrace();
                    return;
                }
                //myMarkerViewLeft.setData(yValForXIndex1);
                myMarkerViewRight.setData(yValForXIndex2);
                mMyBottomMarkerView.setData(time);
                //myMarkerViewLeft.refreshContent(e, mIndicesToHighlight[i]);
                myMarkerViewRight.refreshContent(e, mIndicesToHighlight[i]);
                mMyBottomMarkerView.refreshContent(e, mIndicesToHighlight[i]);
                /*修复bug*/
                // invalidate();
                /*重新计算大小*/
                /*myMarkerViewLeft.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                myMarkerViewLeft.layout(0, 0, myMarkerViewLeft.getMeasuredWidth(),
                        myMarkerViewLeft.getMeasuredHeight());*/
                myMarkerViewRight.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                myMarkerViewRight.layout(0, 0, myMarkerViewRight.getMeasuredWidth(),
                        myMarkerViewRight.getMeasuredHeight());
                mMyBottomMarkerView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                mMyBottomMarkerView.layout(0, 0, mMyBottomMarkerView.getMeasuredWidth(),
                        mMyBottomMarkerView.getMeasuredHeight());

                if(getXAxis().isDrawLabelsEnabled()){
                    if(pos[0]<mMyBottomMarkerView.getWidth()/2){
                        mMyBottomMarkerView.draw(canvas, 0,
                            mViewPortHandler.contentBottom());
                    }else if(pos[0]+mMyBottomMarkerView.getWidth()/2>mViewPortHandler.contentRight()){
                        mMyBottomMarkerView.draw(canvas, mViewPortHandler.contentRight() - mMyBottomMarkerView.getWidth(),
                            mViewPortHandler.contentBottom());
                    }else {
                        mMyBottomMarkerView.draw(canvas, pos[0] - mMyBottomMarkerView.getWidth() / 2,
                            mViewPortHandler.contentBottom());
                    }
                }
                if(pos[1]<myMarkerViewRight.getHeight() / 2){
                    myMarkerViewRight.draw(canvas, mViewPortHandler.contentRight()-myMarkerViewRight.getWidth(),
                        0);
                }else {
                    myMarkerViewRight.draw(canvas, mViewPortHandler.contentRight()-myMarkerViewRight.getWidth(),
                        pos[1] - myMarkerViewRight.getHeight() / 2);
                }
            }
        }
    }

    protected void drawPushLine(Canvas canvas) {
      if(null==minuteHelper)return;
        ArrayList<Double>list=minuteHelper.getPushLine();
        if (list.size() > 0) {
            for (double a : list) {
                float number = (float) a;
                float[] pts = { 1f, number };
                mAxisRendererLeft.getTransformer().pointValuesToPixel(pts);
                Paint paint = mRenderer.getPaintHighlight();
                paint.setColor(ChartThemeUtil.instance().color_avg_line);
                canvas.drawLine(mViewPortHandler.contentLeft(), pts[1], mViewPortHandler.contentRight(),
                    pts[1], paint);

                MyRightMarkerView mPushLine = new MyRightMarkerView(mContext, R.layout.push_line_marker_view,
                    new DecimalFormat("#0.00"));
                mPushLine.setText(number);
                mPushLine.setTextColor(0,ChartThemeUtil.instance().color_avg_line);
                mPushLine.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                mPushLine.layout(0, 0, mPushLine.getMeasuredWidth(), mPushLine.getMeasuredHeight());
                if(pts[1]<mPushLine.getHeight() / 2&&pts[1]>=0){
                    mPushLine.draw(canvas, mViewPortHandler.contentRight() - mPushLine.getWidth(),
                        0);
                }else {
                    mPushLine.draw(canvas, mViewPortHandler.contentRight() - mPushLine.getWidth(),
                        pts[1] - mPushLine.getHeight() / 2);
                }
            }
        }
    }

    /**
     * 设置建仓数据
     * @param lineList
     */
    public void setPushLineList(ArrayList<Double> lineList){
        if(null!=minuteHelper) {
            minuteHelper.setPushLine(lineList);
        }
    }

    public void refreshProductDecimalNum(DecimalFormat decimalFormat){
        myMarkerViewRight.setmFormat(decimalFormat);
    }

    public void noDataView(){
        /*getAxisRight().setAxisMaxValue(0f);*/
        //getAxisRight().setAxisMinValue(0f);
        setTouchEnabled(false);
        LineData lineData=new LineData();
        setData(lineData);
        notifyDataSetChanged();
        invalidate();
    }


    }
